﻿using Microsoft.AspNetCore.Http;
using System.Security.Claims;
using Video.Application.Contract;
using Video.Domain.Shared;

namespace Video.Application.Manage
{
     public class CurrentManage
    {
        //IHttpContextAccessor这个接口的作用是获取Http的上下文，如ID,Name等
        private readonly IHttpContextAccessor _httpContext;
        public CurrentManage(IHttpContextAccessor httpContext)
        {
            _httpContext = httpContext;
        }
        //
        public Guid GetId()
        {
            //如果x的声明的值的类型等于Constant.Id的类型并且不为空，解析为Guid类型并返回，x.Value表示当前声明的值
            var value = _httpContext.HttpContext.User.Claims.FirstOrDefault(x => x.Type == Constant.Id)?.Value;
            if(string.IsNullOrEmpty(value))
            {
                throw new BusinessException("账号异常，请重试", 401);
            }
            return Guid.Parse(value);
        }
        /// <summary>
        /// 获取角色编码
        /// </summary>
        /// <returns></returns>
        public IEnumerable<string> GetRoles()
        {
            var value = _httpContext.HttpContext.User.Claims.Where(x => x.Type == ClaimsIdentity.DefaultIssuer).Select(x=>x.Value);
            return value;
        }
    }
}
